package com.example.controller;

import com.example.pojo.vo.ChatMessageVO;
import com.example.service.SessionMessageService;
import com.example.service.SessionService;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
@AllArgsConstructor
public class HistoryChatController {
    private final SessionService sessionService;
    private final ChatMemory chatMemory;
    private final SessionMessageService messageService;
    
    @GetMapping("/{type}")
    public List<String> getHistorySessionIdList(@PathVariable String type) {
       return sessionService.getHistorySessionIdList(type);
    }
    
    @GetMapping("/{type}/{chatId}")
    public List<ChatMessageVO> getChatHistoryById(@PathVariable String type, @PathVariable String chatId){
        List<Message> messages = chatMemory.get(chatId);
        if (messages == null || messages.isEmpty()){
            return List.of();
        }
        return messages.stream()
                .map(message -> new ChatMessageVO(message.getMessageType().name(), message.getText()))
                .toList();
    }
}
